/*
 * Copyright (c) 2015 elion. All Rights Reserved.
 */
package com.guoyu.project.web.admin;

import java.util.Map;

import javax.annotation.Resource;

import com.guoyu.project.common.pagination.Pagination;
import com.guoyu.project.common.utils.JsonUtil;
import com.guoyu.project.common.web.APIResponse;
import com.guoyu.project.model.Banner;
import com.guoyu.project.service.BannerService;
import com.guoyu.project.service.FileUploadService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.google.common.collect.Maps;

/**
 * banner信息,前端接口BannerController
 *
 * @author: xblibo Sat Jun 27 14:00:13 CST 2015
 * @version 1.0
 */
@Controller
@RequestMapping("admin/banner")
public class BannerController {

    private final static Logger logger = LoggerFactory.getLogger(BannerController.class);

    @Resource
    private BannerService bannerService;

    @Resource
    private FileUploadService fileUploadService;

    @RequestMapping("index")
    public String index() {
        return "admin/banner/index";
    }

    /**
     * banner信息列表页
     *
     * @return
     */
    @RequestMapping("list")
    @ResponseBody
    public Pagination<Banner> list(@RequestParam(defaultValue = "0") int offset,
            @RequestParam(defaultValue = "10") int limit, Model model) {
        Map<String, Object> params = Maps.newHashMap();
        return bannerService.queryBanners(offset, limit, params);
    }

    /**
     * 跳到添加今日资讯填写页
     *
     * @return
     */
    @RequestMapping("add")
    public String add(Model model) {
        return "admin/banner/add";
    }

    /**
     * 保存今日资讯信息
     *
     * @return
     */
    @RequestMapping("save")
    @ResponseBody
    public APIResponse save(@RequestParam(value = "file", required = false) MultipartFile picture, Banner banner) {
        logger.info("提交添加banner信息信息：{}", JsonUtil.encode(banner));
        String filePath = fileUploadService.upload(picture);
        banner.setImage(filePath);
        bannerService.saveBanner(banner);
        return APIResponse.returnSuccess();
    }

    /**
     * 跳到修改信息页面
     *
     * @return
     */
    @RequestMapping("edit")
    public String edit(int bannerId, Model model) {
        Banner banner = bannerService.queryById(bannerId);
     
        model.addAttribute("banner", banner);
        return "admin/banner/edit";
    }

    /**
     * 更新信息
     *
     * @return
     */
    @RequestMapping("update")
    @ResponseBody
    public APIResponse update(@RequestParam(value = "file", required = false) MultipartFile picture, Banner banner) {
        logger.info("提交修改banner信息信息：{}", JsonUtil.encode(banner));
        String filePath = StringUtils.defaultIfBlank(fileUploadService.upload(picture), banner.getImage());
        banner.setImage(filePath);
        bannerService.updateBanner(banner);
        return APIResponse.returnSuccess();
    }

    /**
     * 删除今日资讯信息
     *
     * @return
     */
    @RequestMapping("delete")
    @ResponseBody
    public APIResponse delete(int bannerId) {
        bannerService.deleteById(bannerId);
        return APIResponse.returnSuccess();
    }

}